<template>
  <h2>{{ $route.meta.title }}</h2>

  <el-button type="primary" @click="dialogFormVisible = true">添加商品</el-button>

  <el-input v-model="searchStr" style="width: 240px" placeholder="Please input" />

  商品分类:
  <el-select v-model="selectCate" placeholder="Select" @change="cateChange" style="width: 240px">
    <el-option v-for="item in productCateArr" :key="item" :label="item" :value="item" />
  </el-select>

  <el-button type="primary" @click="searchHandler">搜索</el-button>

  <el-dialog v-model="dialogFormVisible" title="Shipping address" width="700">
    <el-form :model="form">
      <el-form-item label="商品名称" :label-width="200">
        <el-input v-model="form.proname" autocomplete="off" />
      </el-form-item>
      <el-form-item label="商品详情" :label-width="200">
        <el-input v-model="form.desc" autocomplete="off" />
      </el-form-item>

      <el-form-item label="商品价格" :label-width="200">
        <el-input v-model="form.originprice" autocomplete="off" />
      </el-form-item>
      <el-form-item label="商品分类" :label-width="200">
        <el-select v-model="form.category" placeholder="Please select a zone">
          <el-option v-for="item in productCateArr" :key="item" :label="item" :value="item" />
        </el-select>
      </el-form-item>

      <!-- 上传图片组件 -->

      <!-- 双向绑定--保存 所有上传的图片 -->
      <!-- v-model:file-list="fileList" -->
      <!-- action  上传图片的接口 地址 -->
      <!-- name="img"  上传图片接口 要求的参数名-参数值 upload组件会自动 获取我们选中的图片 -->
      <el-form-item label="商品图片" :label-width="200">
        <el-upload
          v-model:file-list="fileList"
          action="http://47.94.6.186:3001/admin/file/upload"
          name="img"
          list-type="picture-card"
          :on-preview="handlePictureCardPreview"
          :on-remove="handleRemove"
        >
          <el-icon><Plus /></el-icon>
        </el-upload>
        <el-dialog v-model="dialogVisible">
          <img style="width: 100%" :src="dialogImageUrl" alt="Preview Image" />
        </el-dialog>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="handlerSendProduct"> 发布 </el-button>
      </div>
    </template>
  </el-dialog>

  <el-table :data="productArr" style="width: 100%">
    <el-table-column prop="proname" label="商品名称" width="180" />
    <el-table-column prop="originprice" label="商品价格" width="180" />
    <el-table-column prop="sales" label="销量" width="180" />
    <!-- <el-table-column prop="desc" label="商品详情" /> -->

    <el-table-column label="商品详情" width="180">
      <template #default="scope">
        <!-- scope.row 数组的元素 -->
        <div style="display: flex; align-items: center">
          <!--文本组件--超出2行不展示 -->
          <el-text line-clamp="2">
            {{ scope.row.desc }}
          </el-text>
        </div>
      </template>
    </el-table-column>

    <el-table-column label="商品详情" width="180">
      <template #default="scope">
        <!-- scope.row 数组的元素 -->
        <div style="display: flex; align-items: center">
          <!--文本组件--超出2行不展示 -->
          <el-image
            loading="lazy"
            :src="item"
            v-for="item in scope.row.imgs"
            :key="item"
            class="img"
            :preview-src-list="scope.row.imgs"
            :preview-teleported="true"
          >
            <template #error>
              <div class="image-slot">
                <el-icon><icon-picture /></el-icon>
              </div>
            </template>
          </el-image>
        </div>
      </template>
    </el-table-column>

    <el-table-column prop="category" label="商品分类" width="180" />

    <el-table-column label="操作" fixed="right" width="180">
      <template #default="scope">
        <el-button size="small" @click="handleEdit(scope.row)"> 编辑 </el-button>
        <el-button size="small" type="danger" @click="handleDelete(scope.row)"> 删除 </el-button>
      </template>
    </el-table-column>
  </el-table>
</template>
<script setup lang="ts">
import {
  productAddRequest,
  productCateLIstRequest,
  productLIstRequest,
  type IProduct,
} from '..//api/productAPI'
import { ref } from 'vue'
import { ElMessage, type UploadProps, type UploadUserFile } from 'element-plus'
import { Picture as IconPicture } from '@element-plus/icons-vue'

// 保存商品列表数据
const productArr = ref<IProduct[]>()
const productCateArr = ref<string[]>()
// 请求列表数据
async function loadProductList() {
  const res = await productLIstRequest({
    count: 1,
    limitNum: 10,
    search: '',
    category: '',
  })
  console.log(res.data)
  productArr.value = res.data.data
}

async function loadProductCateList() {
  const res = await productCateLIstRequest()
  console.log(res.data)
  productCateArr.value = res.data.data
}

loadProductList()
loadProductCateList()

function handleEdit(item: IProduct) {
  console.log(item)
}
function handleDelete(item: IProduct) {}

/*
  商品发布

*/
const dialogFormVisible = ref(false)
const form = ref<IProduct>({
  proname: '',
  proid: '',
  desc: '',
  originprice: 0,
  sales: 0,
  category: '',
  imgs: [],
  stock: 0,
})

function setForm() {
  form.value = {
    proname: '',
    proid: '',
    desc: '',
    originprice: 0,
    sales: 0,
    category: '',
    imgs: [],
    stock: 0,
  }
}

const fileList = ref<UploadUserFile[]>([
  // {
  //   name: 'food.jpeg',
  //   url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100',
  // },
])

const dialogImageUrl = ref('')
const dialogVisible = ref(false)

const handleRemove: UploadProps['onRemove'] = (uploadFile, uploadFiles) => {
  console.log(uploadFile, uploadFiles)
}

const handlePictureCardPreview: UploadProps['onPreview'] = (uploadFile) => {
  dialogImageUrl.value = uploadFile.url!
  dialogVisible.value = true
}
async function handlerSendProduct() {
  //获取表单数据
  console.log(form.value)
  //把fileList中元素中response.fileUrl 提取到新数组中
  const baseURL = 'http://47.94.6.186:3001'
  const imgs = fileList.value.map((v) => baseURL + v.response.fileUrl)
  console.log(imgs)

  form.value.imgs = imgs

  // 调用发布商品接口
  const res = await productAddRequest(form.value)
  console.log(res.data)
  if (res.data.code == '200') {
    ElMessage.success('发布商品成功')
    dialogFormVisible.value = false
    loadProductList()
    //清空表单
    setForm()
  }
}

/*
 商品搜哦

*/
const searchStr = ref('')
async function searchHandler() {
  console.log(searchStr.value)
  //调用搜索接口
  const res = await productLIstRequest({
    count: 1,
    limitNum: 10,
    search: searchStr.value,
    category: selectCate.value,
  })
  console.log(res.data.data)
  productArr.value = res.data.data
}

/*
  分类筛选
*/
const selectCate = ref('')
function cateChange() {
  searchHandler()
}
</script>
<style lang="css" scoped>
.img {
  width: 100px;
}
</style>
